package com.woniuxy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.conmon.MyPageData;
import com.woniuxy.conmon.enums.SystemCode;
import com.woniuxy.pojo.Order;
import com.woniuxy.pojo.dto.MergeOrdersDTO;
import com.woniuxy.pojo.dto.OrderDTO;
import com.woniuxy.pojo.vo.OrderDetailVO;
import com.woniuxy.pojo.vo.StatusCountVO;
import com.woniuxy.service.OrderService;
import com.woniuxy.utils.EasyExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/getAllOrders/{pageSize}/{pageNum}")
    public ResponseEntity<?>getAllOrders(@PathVariable Integer pageSize,
                                        @PathVariable Integer pageNum){
        Page<Order> page = orderService.getAllOrder(pageSize,pageNum);
        List<Order> orders = page.getRecords();
        MyPageData data = new MyPageData(page.getTotal(),orders);

        return ResponseEntity.ok(data);
    }

    @GetMapping("/getOrderByStatus/{orderStatus}")
    public  ResponseEntity<?> getOrderByStatus(@PathVariable Integer orderStatus){

        List<Order> orders = orderService.getOrderByStatus(orderStatus);
        return ResponseEntity.ok(orders);
    }

    @GetMapping("/getOrderStatus")
    public ResponseEntity<?> getOrderStatus(){
        List<Integer>status = orderService.getOrderStatus();
        return ResponseEntity.ok(status);
    }

    @GetMapping("/getOrderSource")
    public  ResponseEntity<?> getOrderSource(){
        List<String>sources = orderService.getOrderSource();
        return ResponseEntity.ok(sources);
    }

    @PostMapping("/selectOrders")
    public ResponseEntity<?> selectOrders(@RequestBody OrderDTO orderDTO){
        List<Order> orders = orderService.selectOrders(orderDTO);

        return ResponseEntity.ok(orders);

    }

    @GetMapping("/getStatusCounts")
    public ResponseEntity<?> getStatusCounts(){
        StatusCountVO vo = orderService.getStatusCounts();
        return ResponseEntity.ok(vo);
    }

    @PostMapping("/download")
    public void download(HttpServletResponse response,
                                      @RequestBody List<Long> ids){
       List<Order> orders = orderService.getOrderByIds(ids);
        EasyExcelUtil.export(response,"订单数据","模板",orders,Order.class);

    }

    @DeleteMapping("/deleteOrder/{id}/{description}")
    public ResponseEntity<?> deleteOrder(@PathVariable Long id,
                                         @PathVariable String description){
        orderService.deleteOrder(id,description);
        return ResponseEntity.ok(SystemCode.CLOSEORDER_SUCCESS.getMessage());

    }

    @PostMapping("/mergeOrders")
    public ResponseEntity<?> mergeOrders(@RequestBody MergeOrdersDTO mergeOrdersDTO){

        ResponseEntity<?> result = orderService.mergeOrders(mergeOrdersDTO);

        return ResponseEntity.ok(result);

    }

    @GetMapping("/getOrderDetail/{id}")
    public ResponseEntity<?> getOrderDetail(@PathVariable Long id){
        OrderDetailVO orderDetailVO = orderService.getOrderDetail(id);
        return ResponseEntity.ok(orderDetailVO);
    }
}
